package com.wisdytech.linkdcs.equipLifeCycle.maintain.action;


import com.wisdytech.common.handler.ResourceMatchingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.dto.MaintenancePlanDTO;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.model.MaintenancePlan;
import com.wisdytech.linkdcs.system.model.SysUser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.service.IMaintenancePlanService;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.vo.MaintenancePlanVO;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenbin
 * @since 2018-06-10
 */
@Controller
@RequestMapping("/maintain/maintenancePlan")
public class MaintenancePlanController {

	@Resource
	private IMaintenancePlanService iMaintenancePlanService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
	/**
     * 
     * <p>Description: 设备保养计划</p>
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String maintainItem(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "lifeCycle/equipLifeCycle/maintain/plan";
    }
    
    /**
     * 分页条件查询设备保养计划列表信息
     * @param modelCode 设备型号
     * @param filter 分页参数
     * @return 设备保养计划列表信息
     * @throws IOException 
     */
    @RequestMapping(value = "/listModelPlanByFilter/get.mvc")
    @ResponseBody	
    public StandardTableResult<MaintenancePlanVO> listModelPlanByFilter(String modelCode,Filter filter) throws IOException{
    	PageInfo<MaintenancePlan> maintenancePlans =this.iMaintenancePlanService.listModelPlanByFilter(modelCode,filter);
        //转换为前台结果集
        List<MaintenancePlan> plansList=maintenancePlans.getList();
        List<MaintenancePlanVO> plansVOList=new ArrayList<MaintenancePlanVO>();
        for(MaintenancePlan plan:plansList){
        	plansVOList.add(DataTransformUtil.changeModelPlanVO(plan));
        }
        return new StandardTableResult<MaintenancePlanVO>(maintenancePlans.getTotal(),plansVOList);
    }
    
    /**
     * <p>Description: 保存保养周期性计划</p> 
     * @param maintenancePlanDTO 保养计划对象
     * @return 处理结果对象
     */
    @RequestMapping(value = "/savePlan/save.mvc")
    @ResponseBody
    public StandardResult savePlan(MaintenancePlanDTO maintenancePlanDTO, HttpSession session) {
       SysUser userSession = BaseUtil.getCurrentUser();
       String loginName = userSession.getUsername();
        if (StringUtils.isBlank(maintenancePlanDTO.getPkId())) {
        	iMaintenancePlanService.addPlan(maintenancePlanDTO, loginName);
        }else {
        	iMaintenancePlanService.updatePlan(maintenancePlanDTO, loginName);
        }
        return StandardResult.getSuccessResult();
    }
    
    
    
    /**
     * <p>Description:查询设备保养计划信息列表</p> 
     * @param pkId 唯一id
     * @return 保养计划信息
     */
    @RequestMapping(value = "/getPlanById/get.mvc")
    @ResponseBody
    public StandardResult getPlanById(String pkId){
    	MaintenancePlan maintenancePlan = iMaintenancePlanService.getPlanById(pkId);
        return StandardResult.getSuccessResult(maintenancePlan);
    }
    
    /**
     * 通过id批量删除设备保养信息
     * @param id 主键id
     * @return 删除结果集
     */
    @RequestMapping(value = "/deletePlanById/del.mvc")
    @ResponseBody
    public StandardResult deletePlanById(String pkId) {
    	iMaintenancePlanService.deletePlanById(pkId);
        return StandardResult.getSuccessResult();
    }
}

